import { getCurrentInstance } from 'vue'

import { useCode } from '@/hooks/code'
import { deleteFileAPI } from '@/api'
import { useUserStore } from '@/stores/user'

const userStore = useUserStore()

const { tackleCode } = useCode()

export function useFile() {
  const proxy: any = getCurrentInstance()?.proxy
  // 删除文件
  const deleteFile = async (data: { id: number | string; url: string | string[] }) => {
    const request = {
      token: userStore.token,
      filesId: data.id,
      filesUrl: data.url,
    }

    const res: any = await deleteFileAPI(request)
    if (tackleCode(res.code)) {
      console.log(res)
      proxy.$message({
        type: 'primary',
        message: '删除成功',
      })
    }
  }

  return {
    deleteFile,
  }
}
